﻿var cloudninja = cloudninja || {};
cloudninja.metering = cloudninja.metering || {};
cloudninja.metering.common = cloudninja.metering.common || {};


cloudninja.metering.common.Monthes = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];



if (!Array.prototype.indexOf) {
    Array.prototype.indexOf = function (elt /*, from*/) {
        var len = this.length;

        var from = Number(arguments[1]) || 0;
        from = (from < 0)
         ? Math.ceil(from)
         : Math.floor(from);
        if (from < 0)
            from += len;

        for (; from < len; from++) {
            if (from in this &&
          this[from] === elt)
                return from;
        }
        return -1;
    };
}

$.editable.addInputType('monthYear', {
    element: function () {
        var monthSelect = $('<select id="monthList">');
        var yearSelect = $('<select id="yearList">');

        for (var i = 0; i < cloudninja.metering.common.Monthes.length; i++) {
            var option = $('<option>').val(i).append(cloudninja.metering.common.Monthes[i]);
            monthSelect.append(option);
        }
        $(this).append(monthSelect);

        var currentYear = new Date().getFullYear();
        for (var i = 0; i < 3; i++) {
            var year = currentYear - i;
            var option = $('<option>').val(year).append(year);
            yearSelect.append(option);
        }
        $(this).append(yearSelect);

        var hidden = $('<input type="hidden">');
        $(this).append(hidden);
        return (hidden);
    },

    submit: function () {
        var value = (+ $("#monthList", this).val() + 1) + " " + $("#yearList", this).val();
        $("input", this).val(value);
    },

    content: function (value) {
        var pair = $(value).text().split(" ");

        var month = pair[0];
        var year = pair[1];

        $("#monthList", this).val(cloudninja.metering.common.Monthes.indexOf(month));
        $("#yearList", this).val(year);
    }
});


$(document).ready(function () {
    $(".pageHeader.secondary").click(function () {
        window.history.back();
    });

    initTimePeriodSelector();
});

function initTimePeriodSelector() {
    $("#timePeriodContainer").editable(function (value) {
        // value is "month year" in format of "9, 2011" with zero-based month (so October 2011 in this case)

        var pair = value.split(' ');

        var params = $.deparam.querystring();
        params["month"] = pair[0];
        params["year"] = pair[1];
        document.location.href = $.param.querystring(document.location.href, params);
    }, {
        type: "monthYear",
        submit: "OK",
        style: "display: inline",
        tooltip: "Click to edit..."
    });

    if ($("#timePeriodContainer .label").length > 0)
        $("#timePeriodContainer .label").text(cloudninja.metering.common.Monthes[month - 1] + ' ' + year);
}

